<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/SystemHomePage' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>在线帮助</el-breadcrumb-item>
      <el-breadcrumb-item>用户提问</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getCommentList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGrabble"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 用户列表区 -->
      <el-table :data="currentPageData" stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="时间" prop="date"></el-table-column>
        <el-table-column label="用户名" prop="commentUsers"></el-table-column>
        <el-table-column label="内容" prop="content"></el-table-column>
        <el-table-column label="点赞">
          <template slot-scope="scope">
            <el-rate
              v-model="
                nice[
                  scope.$index + (queryInfo.pageNum - 1) * queryInfo.pageSize
                ]
              "
              disabled
              show-score
              text-color="#ff9900"
            >
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" @click="updateById(scope.row._id)"
              >回复</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog title="回复评论" :visible.sync="dialogFormVisible">
        <el-form :model="CommentForm" ref='CommentForm'>
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input
              disabled
              v-model="CommentForm.commentUsers"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="内容" :label-width="formLabelWidth">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="CommentForm.content"
            >
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeFrom">取 消</el-button>
          <el-button type="primary" @click="removeById(CommentForm.id)">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageNum: 1, // 当前页数，默认为1
        pageSize: 5, // 每页显示数量
      },
      CommentList: [], //所有数据
      currentPageData: [], //当前页显示内容
      total: 1, //页面所有条数
      FuzzyQuery: null,
      userID: "",
      nice: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      CommentForm: {
        commentUsers: "",
        id: "",
        content:'',
      },
      textarea: "",
      formLabelWidth: "120px",
    };
  },
  mounted() {
    this.getCommentList();
  },
  methods: {
    //显示评论列表
    async getCommentList() {
      const { data: result } = await this.$http.get("CommentList", {
        params: this.queryInfo,
      });
      if (!result.ret) return this.$message.error("用户列表失败");
      this.CommentList = result.data;
      this.currentPageData = result.data;
      let newArr = this.currentPageData.map((item) => item.nice);
      for (let i = 0; i < newArr.length; i++) {
        this.nice.push(Number(newArr[i]));
      }
      //显示共多少条
      this.total = this.CommentList.length;
      this.handleSizeChange();
    },
    //评论查询
    async getGrabble() {
      const getGrabbleValue = await this.$http.post("GrabbleComment", {
        params: this.queryInfo.query,
      });
      const FuzzyQuerydata = getGrabbleValue.data.data.GrabbleComment;
      if (FuzzyQuerydata != "") {
        this.currentPageData = FuzzyQuerydata;
      } else {
        this.currentPageData = this.CommentList;
      }
    },
    // 获取想要删除值的id
    async removeById(id) {
      const { data: result } = await this.$http.delete("removeComment", {
        data: { id },
      });
      if (result.errorCode !== 0) {
        return this.$Message.error("评论失败");
      } else {
        this.$Message.success("评论成功");
        this.dialogFormVisible = false;
        this.getCommentList();
      }
    },
    //监听pageSize改变是事件
    handleSizeChange(newSize) {
      //获取当前页面共多少条信息
      if (newSize) {
        this.queryInfo.pageSize = newSize;
      } else {
        this.queryInfo.pageSize;
      }
      let begin = (this.queryInfo.pageNum - 1) * this.queryInfo.pageSize;
      let end = this.queryInfo.pageNum * this.queryInfo.pageSize;
      this.currentPageData = this.CommentList.slice(begin, end);
    },
    //监听页码值改变是事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.handleSizeChange(this.pageSize);
    },
    closeFrom(){
      this.dialogFormVisible = !this.dialogFormVisible
      this.addDialogClosed()
    },
    //监听添加用户对话框关闭事件清空内容
    addDialogClosed() {
      if (this.$refs.CommentForm != undefined) {
        this.CommentForm.content = "";
      }
    },
    async updateById(id) {
      this.dialogFormVisible = true;
      const { data: result } = await this.$http.put("ListOneComment", {
        id: id,
      });
      (this.CommentForm.commentUsers = result.commentUsers),
        (this.updateDialogVisible = true);
      this.CommentForm.id = id;
    },
  },
};
</script>
<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>